#!/bin/bash

# shellcheck disable=SC2120

# Provide source directive to shellcheck.
# shellcheck source=meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/power-cmd
source /usr/libexec/phosphor-state-manager/power-cmd
CHASSIS_ID=$1

CHASSIS_BUS=$((CHASSIS_ID - 1))
IO_EXP_P0_PWRGD_R_OUT=18
GPIOCHIP_IO_EXP_HOST_POWER_STATUS=$(basename "/sys/bus/i2c/devices/$CHASSIS_BUS-0023/"*gpiochip*)

EID=$((CHASSIS_ID * 10))

get_host_status() {
    host_status=$(gpio_get "$GPIOCHIP_IO_EXP_HOST_POWER_STATUS" "$IO_EXP_P0_PWRGD_R_OUT")

    if [ "$host_status" = "gpioget failed" ]; then
        echo "Failed to get host status"
        exit 1
    fi
}

# Get initial host status
get_host_status

# Current power is ON, cycle do OFF to ON. If current power is OFF then do ON
if [ "$host_status" == "$STATE_ON" ]; then
    # Set state effecter state: Entity ID 0x0000 for host power control, 0x2 for power off
    pldmtool raw -d 0x80 0x02 0x39 0x00 0x00 0x01 0x00 0x02 -m "$EID"

    # Set host state to off
    busctl set-property "$HOST_BUS_NAME""$CHASSIS_ID" "$HOST_OBJ_PATH""$CHASSIS_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_OFF_PROPERTY"

    # Wait for the host to power off
    sleep 10s

    # Get host status again after attempting to power off
    get_host_status

    if [ "$host_status" != "$STATE_OFF" ]; then
        busctl set-property "$HOST_BUS_NAME""$CHASSIS_ID" "$HOST_OBJ_PATH""$CHASSIS_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_ON_PROPERTY"
        msg="Do power cycle fail, fail to set host$CHASSIS_ID power off"
        echo "${msg}"
        add_sel "${msg}"
        exit 1
    fi
fi

if [ "$host_status" != "$STATE_ON" ]; then
    # Set state effecter state: Entity ID 0x0000 for host power control, 0x1 for power on
    pldmtool raw -d 0x80 0x02 0x39 0x00 0x00 0x01 0x00 0x01 -m "$EID"
    sleep 2s

    # Get host status again after attempting to power on
    get_host_status

    if [ "$host_status" != "$STATE_ON" ]; then
        msg="Do power cycle, failed to set host$CHASSIS_ID power on"
        echo "${msg}"
        add_sel "${msg}"
        exit 1
    fi

    # Set host state to on
    busctl set-property "$HOST_BUS_NAME""$CHASSIS_ID" "$HOST_OBJ_PATH""$CHASSIS_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_ON_PROPERTY"
fi

msg="Host$1 power cycle success"
echo "${msg}"
add_sel "${msg}"
exit 0;
